<?php
namespace App\Http\Controllers\Business\ShopWeb;

use App\Models\ShopUser;
use App\Models\Shop;
use App\Models\Order;
use App\Models\ShopSalesman;
use App\Models\ShopSalesmanDetail;
use Illuminate\Support\Facades\Redis;

class PublicBusiness {
    //通过商家id  获取商家名称,
    public static function getShopNameByShopId($shopId) {
        $shopName = Shop::where('id', $shopId)->pluck('name')->first();
        return $shopName;
    }
    //通过shopid获取商家信息
    public static function getShopDetailByShopId($shopId) {
        if(!$shopId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $data = Shop::where('id', $shopId)->first();
        return ['status'=>'success', 'data'=>$data];
    }

    //获取登录帐号下所有商家信息
    public static function getAllShopsByShopUserId($shopUserId) {
        if(!$shopUserId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $shopIdString = ShopUser::where('id', $shopUserId)->pluck('shopid')->first();
        if(!$shopIdString) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $data = [];
        $shopIds = explode(',', $shopIdString);
        if(count($shopIds) > 1) {
            array_push($data, [
                'shopid' => $shopIdString,
                'shopname' => '全部',
                'address' => ''
            ]);

        }
        foreach($shopIds as $shopId) {
            $shop = Shop::where('id', $shopId)->select('name', 'address')->first();
            if($shop) {
                array_push($data, [
                    'shopid' => $shopId,
                    'shopname' => $shop['name'],
                    'address' => $shop['address']
                ]);
            }
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //通过所有商家ids获取统计数据
    public static function getAllShopsStatistic($shopIds) {
        if(empty($shopIds)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $today = date('Y-m-d') . " 00:00:00";
        //今天订单数
        $todayOrderCount = Order::whereIn('shopid', $shopIds)->where('created_at','>=',$today)->get()->count();
        //今日订单消费金额
        $todayOrderSum = Order::whereIn('shopid', $shopIds)->where('created_at','>=', $today)->sum('orderamount');
        //今日地推注册数
        $todaySalesmanCount = ShopSalesmanDetail::whereIn('shopid', $shopIds)->where('created_at', '>=', $today)->where('type', 'register')->where('getmoney', '<>', 0.00)->get()->count();
        //今日地推收入(元)
        $todaySalesmanSum = ShopSalesmanDetail::whereIn('shopid', $shopIds)->where('created_at', '>=', $today)->where('type', 'register')->where('getmoney', '<>', 0.00)->sum('getmoney');
        $data = [
            'todayordercount' => $todayOrderCount,
            'todayordersum' => $todayOrderSum,
            'todaysalesmancount' => $todaySalesmanCount,
            'todaysalesmansum' => $todaySalesmanSum
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //通过单一商家id获取统计
    public static function getSingleShopStatistic($shopId) {
        if(!$shopId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $data = Shop::where('id', $shopId)->select('id as shopid','name', 'status', 'todayevent', 'nextevent')->first();
        if(!$data) {
            return ['status'=>'failed', 'message'=>'获取信息错误'];
        }
        $today = date('Y-m-d') . " 00:00:00";
        //今天订单数
        $todayOrderCount = Order::where('shopid', $shopId)->where('created_at','>=',$today)->get()->count();
        //今日订单消费金额
        $todayOrderSum = Order::where('shopid', $shopId)->where('created_at','>=', $today)->sum('orderamount');
        //今日地推注册数
        $todaySalesmanCount = ShopSalesmanDetail::where('shopid', $shopId)->where('created_at', '>=', $today)->where('type', 'register')->where('getmoney', '<>', 0.00)->get()->count();
        //今日地推收入(元)
        $todaySalesmanSum = ShopSalesmanDetail::where('shopid', $shopId)->where('created_at', '>=', $today)->where('type', 'register')->where('getmoney', '<>', 0.00)->sum('getmoney');
        $data ['todayordercount'] = $todayOrderCount;
        $data['todayordersum'] = $todayOrderSum;
        $data['todaysalesmancount'] = $todaySalesmanCount;
        $data['todaysalesmansum'] = $todaySalesmanSum;
        return ['status'=>'success', 'data'=>$data];
    }

}